package com.stiff.web.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.stiff.common.bean.EasyUIResult;
import com.stiff.common.service.ApiService;
import com.stiff.manage.pojo.Content;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class IndexService {
    @Value("${TAOTAO_MANAGE_URL}")
    private String TAOTAO_MANAGE_URL;
    @Value("${INDEX_AD_URL}")
    private String INDEX_AD_URL;
    @Autowired
    private ApiService apiService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    public String queryIndexAD(String url,Map<String,String> params){
        try {
            String jsonData = this.apiService.doGet(url,params);
            if(StringUtils.isEmpty(jsonData)){
                return null;
            }
            return jsonData;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

//    public String queryIndexAD1() {
//        Map<String,String> params = new HashMap<>();
//        params.put("categoryId","12");
//        params.put("page","1");
//        params.put("rows","6");
//        String jsonData = queryIndexAD(TAOTAO_MANAGE_URL+INDEX_AD_URL,params);
//        try {
//            JsonNode jsonNode = MAPPER.readTree(jsonData);
//            ArrayNode rows = (ArrayNode) jsonNode.get("rows");
//            List<Map<String,Object>> result = new ArrayList<>();
//            for (JsonNode row : rows){
//                Map<String,Object> map = new LinkedHashMap<>();
//                map.put("srcB",row.get("pic2").asText());
//                map.put("height",240);
//                map.put("alt",row.get("title").asText());
//                map.put("width",670);
//                map.put("src",row.get("pic").asText());
//                map.put("widthB",550);
//                map.put("href",row.get("url").asText());
//                map.put("heightB",240);
//                result.add(map);
//            }
//            return MAPPER.writeValueAsString(result);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
    public String queryIndexAD1() {
        Map<String,String> params = new HashMap<>();
        params.put("categoryId","12");
        params.put("page","1");
        params.put("rows","6");
        String jsonData = queryIndexAD(TAOTAO_MANAGE_URL+INDEX_AD_URL,params);
        try {
            EasyUIResult easyUIResult = EasyUIResult.formatToList(jsonData, Content.class);
            List<Map<String,Object>> result = new ArrayList<>();
            for (Content row : (List<Content>)easyUIResult.getRows()){
                Map<String,Object> map = new LinkedHashMap<>();
                map.put("srcB",row.getPic2());
                map.put("alt",row.getTitle());
                map.put("src",row.getPic());
                map.put("href",row.getUrl());
                map.put("height",240);
                map.put("width",670);
                map.put("widthB",550);
                map.put("heightB",240);
                result.add(map);
            }
            return MAPPER.writeValueAsString(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String queryIndexAD2() {
        Map<String,String> params = new HashMap<>();
        params.put("categoryId","13");
        params.put("page","1");
        params.put("rows","1");
        String jsonData = queryIndexAD(TAOTAO_MANAGE_URL+INDEX_AD_URL,params);
        try {
            EasyUIResult easyUIResult = EasyUIResult.formatToList(jsonData, Content.class);
            List<Map<String,Object>> result = new ArrayList<>();
            for (Content row : (List<Content>)easyUIResult.getRows()){
                Map<String,Object> map = new LinkedHashMap<>();
                map.put("srcB",row.getPic2());
                map.put("alt",row.getTitle());
                map.put("src",row.getPic());
                map.put("href",row.getUrl());
                map.put("height",70);
                map.put("width",310);
                map.put("widthB",210);
                map.put("heightB",70);
                result.add(map);
            }
            return MAPPER.writeValueAsString(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}